﻿CREATE PROCEDURE [acms].[UserPostVote_Put]
	@Id int, 
	@UserId int,
	@PostId int,
	@Score decimal(5, 4),
	@Created datetime,
	@ConcurencyId rowversion,
	@IdOut int output,
	@UsernameOut varchar(50) output,
	@FirstNameOut varchar(50) output,
	@LastNameOut varchar(50) output,
	@ConcurencyIdOut rowversion output
AS
	if exists(Select null from UserPostVote with (nolock) Where UserID = @UserId and PostId = @PostId) begin
		update UserPostVote 
		set Score = @Score
		where UserID = @UserId and PostId = @PostId and ConcurencyId = @ConcurencyId
		
		if(@@rowcount = 0)
			return -100
	end
	else begin
		insert into UserPostVote
			(UserID, PostId, Score, Created)
		values
			(@UserID, @PostId, @Score, @Created)
	end
	
	select @IdOut = Id, @UserNameOut = UserName, @FirstNameOut = FirstName, @LastNameOut = LastName, @ConcurencyIdOut = ConcurencyId from UserPostVote_View with (nolock) Where UserID = @UserId and PostId = @PostId
RETURN 0;